package P1763;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Problema da disciplina de Estrutura de Dados e Complexidade de Algoritmos da
 * Pos Graduacao do Periodo 2011.1
 *
 * @see http://br.spoj.pl/problems/SUDOIME/
 * @author Elenilson Vieira - 111100371 - elenilson[at]elenilsonvieira.com
 * @since 9 de maio de 2011
 */
public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

        int amount = Integer.parseInt(reader.readLine().trim());

        for (int i = 0; i < amount; i++) {
            boolean sudoku = true;

            List<Set<Integer>> columns = new ArrayList<Set<Integer>>();
            List<Set<Integer>> arrays = new ArrayList<Set<Integer>>();

            /*** Itera nas linhas ***/
            for(int rowPosition = 0; rowPosition < 9; rowPosition++){
                Set<Integer> row = new HashSet<Integer>();
                String rowValues[] = reader.readLine().split(" ");

                /*** Itera na linha pegando os valores das colunas ***/
                for (int columnPosition = 0; columnPosition < 9; columnPosition++) {
                    int value = Integer.parseInt(rowValues[columnPosition]);
                    
                    /*** Para verificacao da linha ***/
                    row.add(value);

                    /*** Para verificacao das colunas ***/

                    if(columns.size() != 9){
                        columns.add(columnPosition, new HashSet<Integer>());
                    }

                    Set<Integer> setColumn = columns.get(columnPosition);
                    
                    setColumn.add(value);

                    /*** Para verificacao de cada matriz ***/
                    int arrayPosition = -1;

                    /*** Primeira coluna das 3 das 9 matrizes ***/
                    if(rowPosition < 3){
                        if(columnPosition < 3){
                            arrayPosition = 0;
                        }
                        else if(columnPosition < 6)
                        {
                            arrayPosition = 1;
                        }else{
                            arrayPosition = 2;
                        }
                    }
                    else if(rowPosition < 6)
                    {
                        if(columnPosition < 3){
                            arrayPosition = 3;
                        }
                        else if(columnPosition < 6)
                        {
                            arrayPosition = 4;
                        }else{
                            arrayPosition = 5;
                        }
                    }
                    else
                    {
                        if(columnPosition < 3){
                            arrayPosition = 6;
                        }
                        else if(columnPosition < 6)
                        {
                            arrayPosition = 7;
                        }else{
                            arrayPosition = 8;
                        }
                    }


                    if(arrays.size() == arrayPosition){
                        arrays.add(arrayPosition, new HashSet<Integer>());
                    }

                    Set<Integer> array = arrays.get(arrayPosition);

                    array.add(value);
                }

                if(row.size() != 9){
                    sudoku = false;
                    break;
                }
            }

            /*** Verifica as colunas ***/
            if(sudoku){
                for (int j = 0; j < 9; j++) {
                    if(columns.get(j).size() != 9){
                        sudoku = false;
                        break;
                    }
                }
            }

            /*** Verifica os mini-arrays ***/
            if(sudoku){
                for (int j = 0; j < 9; j++) {
                    if(arrays.get(j).size() != 9){
                        sudoku = false;
                        break;
                    }
                }
            }

            System.out.println(String.format("Instancia %d\n%s", i + 1, sudoku ? "SIM" : "NAO"));
        }
    }

}
